<script type="text/html" template>
        <link rel="stylesheet" href="{{ layui.setter.base }}style/css/reset.css">
        <link rel="stylesheet" href="{{ layui.setter.base }}style/css/patrol/plan.css">
</script>
    <style>
        .nextTable {
            width: 100%;
        }
        table thead tr th,
        table tbody tr .t {
            /* height: 30px; */
            vertical-align: middle;
            text-align: center;
            white-space: nowrap;
            /*规定段落的文本不进行换行*/
            overflow: hidden;
            /*超出隐藏*/
            text-overflow: ellipsis;
            /*隐藏的字符用省略号表示  IE*/
            -moz-text-overflow: ellipsis;
            /*隐藏的字符用省略号表示  火狐*/
        }
    
        table {
            table-layout: fixed;
            /*table的内部布局固定大小，才可以通过td的width控制宽度*/
            word-wrap: break-word;
            /*允许长单词换到下一行*/
        }
    
        .footer-btn:nth-child(2) {
            background: red;
        }
    </style>
    <div class="layui-containner">
        <div class="layui-fluid">
            <div class="layui-title">巡检工单提交</div>
            <ul class="box layui-row layui-form" lay-filter="form">
                <li class="layui-form layui-col-md4 layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">工单编号: </label>
                        <div class="layui-input-block">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">工单名称:</label>
                        <div class="layui-input-block">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">工单状态：</label>
                        <div class="layui-input-block">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">巡检计划名称：</label>
                        <div class="layui-input-block">
                        </div>
                    </div>
                </li>
                <li class="layui-form layui-col-md4 layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">巡检开始时间：</label>
                        <div class="layui-input-block startTime" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"> 巡检截止时间：</label>
                        <div class="layui-input-block endtime" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"> 巡检耗时：</label>
                        <div class="layui-input-block">
                        </div>
                    </div>
                </li>
                <li class="layui-form layui-col-md4 layui-col-sm4 xun">
                    <div>巡检人: <span></span></div>
                    <div>位置范围：<span></span></div>
                    <div>巡检说明：<span></span></div>
                </li>
            </ul>
        </div>
        <div class="layui-fluid">
            <div class="layui-title">
                <span class="layui-title-inner">设备基本信息</span>
            </div>
            <table class="layui-table" lay-size="sm">
                <thead>
                    <tr>
                        <th>设备编号</th>
                        <th>设备名称</th>
                        <th>设备类型</th>
                        <th>供应商</th>
                        <th>设备系列</th>
                        <th>设备型号</th>
                        <th style="width: 50%;">巡检项目</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
            <footer class="footer layui-btn-containner">
                <button class="footer-btn layui-btn layui-btn-radius layui-btn-primary submit1 layui-bg-red">提交</button>
                <button class="footer-btn layui-btn layui-btn-radius layui-btn-primary cancel">取消</button>
            </footer>
        </div>
    </div>
    
    <script>
      layui.use(['table', 'admin', 'laypage', 'laytpl', 'tree', 'form', 'api', 'view', 'laydate', 'utill'], function () {
    var $ = layui.$,
        admin = layui.admin,
        view = layui.view,
        api = layui.api,
        table = layui.table,
        setter = layui.setter,
        laytpl = layui.laytpl,
        utill = layui.utill,
        tree = layui.tree,
        laydate = layui.laydate,
        form = layui.form;
        form.render();
            let planId = layui.router().hash.split('=')[1]
            admin.req({
                type: 'post',
                url: layui.api.host + '/order/selectPollingOrderDetails.json',
                dataType: 'json',
                contentType: "application/json",
                data:JSON.stringify({id:planId}),
                success: function (res) {
                let planDetails = res.data.equip // 表格数据
                let planid = res.data.fil // 头部信息 
                let table = `<tr>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td>
                                            <table class="nextTable">
                                                <thead>
                                                    <tr>
                                                        <td>项目名称</td>
                                                        <td>项目说明</td>
                                                        <td>单位</td>
                                                        <td>最高标准值</td>
                                                        <td>最低标准值</td>
                                                        <td>是否异常</td>
                                                        <td>巡检结果</td>
                                                    </tr>
                                                </thead>
                                            </table>
                                        </td>
                                    </tr>`;
                if(planid.fil05 == 0 && planid.fil05 != null){
                    planid.fil05 = '未提交';
                    let html = `<button class="footer-btn layui-btn layui-btn-radius layui-btn-primary submit layui-bg-blue">保存</button>`;
                    let ipt = `<input type="text" id="endTime" autocomplete="off" class="layui-input" name="endTime">`;
                    let ipt1 = `<input type="text" id="startTime" autocomplete="off" class="layui-input" name="startTime">`;
                    $('.endtime').append(ipt);
                    $('.startTime').append(ipt1);
                    $('.submit1').after(html);
                    form.render()
                    $('.submit').on('click', function () {
                        // 进行处理
                        let project = []
                        let number = $('select').length
                        if (!+number) return
                        for (let i = 0; i < number; i++) {
                            project.push({
                                tcProjDsc: $('select').eq(i).parent().next().find('input').val(), // 说明
                                tcProjId: $('select').eq(i).parent().next().find('input').attr('data-id'), // id
                                tcProjStatus: $('select').eq(i).val() // 是否成功 
                            })
                        }
                        admin.req({
                            type: 'post',
                            url: layui.api.host + '/order/insertXJandBY.json',
                            dataType: 'json',
                            contentType: "application/json",
                            data:JSON.stringify({     //修改
                                project,
                                fil12: $('#startTime').val(),
                                fil13: $('#endTime').val(),
                                tcReqId: planId,
                                fil05: 0
                            }),
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.open({
                                        content:res.message
                                    });
                                    setTimeout(function(){
                                        location.hash = '/patrol/cash';
                                    },1000)
                                } else {
                                    layer.open({
                                        content:res.message
                                    });
                                }
                            }
                        })
                    });
                       // 提交
                       $('.submit1').on('click', function () {
                        // 进行处理
                        let formData = form.val('form');
                        let project = []
                        let number = $('select').length
                        if (!+number) return
                        for (let i = 0; i < number; i++) {
                            project.push({
                                tcProjDsc: $('select').eq(i).parent().next().find('input').val(), // 说明
                                tcProjId: $('select').eq(i).parent().next().find('input').attr('data-id'), // id
                                tcProjStatus: $('select').eq(i).val() // 是否成功 
                            })
                        }
                        if(formData.endTime != '' && formData.startTime != ''){
                            admin.req({
                                    type: 'post',
                                    url: layui.api.host + '/order/submitXJandBY1.json',
                                    dataType: 'json',
                                    contentType: "application/json",
                                    data:JSON.stringify({     //修改
                                        project,
                                        fil12: formData.startTime,
                                        fil13: formData.endTime,
                                        tcReqId: planId
                                    }),
                                    success: function (res) {
                                        if (res.code == 0) {
                                            layer.open({
                                                content:res.message
                                            });
                                            setTimeout(function(){
                                                location.hash = '/patrol/cash';
                                            },1000)
                                        } else {
                                            layer.open({
                                                content:res.message
                                            });
                                        }
                                    }

                                })
                        }else if(formData.endTime == ''){
                            layer.open({
                                content: '请选择巡检截止时间！'
                            });
                        }else if(formData.startTime == ''){
                            layer.open({
                                content: '请选择巡检开始时间！'
                            });
                        }
                    
                    })
                }else if (planid.fil05 == 1) {
                    planid.fil05 = '未开始';
                } else if (planid.fil05 == 2) {
                    $('.submit1').on('click', function () {
                        // 进行处理
                        let formData = form.val('form');
                        let project = []
                        let number = $('select').length
                        if (!+number) return
                        for (let i = 0; i < number; i++) {
                            project.push({
                                tcProjDsc: $('select').eq(i).parent().next().find('input').val(), // 说明
                                tcProjId: $('select').eq(i).parent().next().find('input').attr('data-id'), // id
                                tcProjStatus: $('select').eq(i).val() // 是否成功 
                            })
                        }
                        var decs = $('[name="title"]').val();
                        if(formData.endTime != '' && formData.startTime != '' && decs != ''){
                            admin.req({
                                    type: 'post',
                                    url: layui.api.host + '/order/submitXJandBY1.json',
                                    dataType: 'json',
                                    contentType: "application/json",
                                    data:JSON.stringify({     //修改
                                        project,
                                        fil12: formData.startTime,
                                        fil13: formData.endTime,
                                        tcReqId: planId
                                    }),
                                    success: function (res) {
                                        if (res.code == 0) {
                                            layer.open({
                                                content:res.message
                                            });
                                            setTimeout(function(){
                                                location.hash = '/patrol/cash';
                                            },1000)
                                        } else {
                                            layer.open({
                                                content:res.message
                                            });
                                        }
                                    }

                                })
                        }else if(formData.endTime == ''){
                            layer.open({
                                content: '请选择巡检截止时间！'
                            });
                        }else if(formData.startTime == ''){
                            layer.open({
                                content: '请选择巡检开始时间！'
                            });
                        }else if(decs == '' || decs == undefined){
                            layer.open({
                                content: '请填写巡检结果！'
                            });
                        }
                    
                    })
                    planid.fil05 = '待执行';
                } else if (planid.fil05 == 7) {
                    planid.fil05 = '超时';
                }
                else if (planid.fil05 == 8) {
                    planid.fil05 = '超时完成';
                }
                else if (planid.fil05 == 9) {
                    planid.fil05 = '已完成';
                }
                $('.layui-input-block').eq(0).html(planid.fil01)
                $('.layui-input-block').eq(1).html(planid.tcReqName)
                $('.layui-input-block').eq(2).html(planid.fil05)
                $('.layui-input-block').eq(3).html(planid.tcReqName)
                if(planid.fil05 == '未提交'){
                    form.val('form',{
                        'endTime': planid.fil13,
                        'startTime': planid.fil12
                    });
                    var startTime = laydate.render({
                            elem: '#startTime'
                            ,type: 'datetime' //时间格式：yyyy/MM/dd HH:mm:ss
                            ,done: function(value, date, endDate) {
                                endTime.config.min='1900-1-1';
                                    startTime.config.max=endTime.config.max;
                                endTime.config.min = {
                                    year: date.year,
                                    month: date.month - 1,//重点！！！
                                    date: date.date,
                                    hours:date.hours, 
                                    minutes:date.minutes, 
                                    seconds:date.seconds
                                }
                                var stime = new Date(Date.parse(value.replace("-", "/")));            
                                var etime = new Date(Date.parse($("#endTime").val().replace("-", "/")));   
                                if(stime>etime){ 
                                    $("#endTime").val(value);
                                }
                            }
                        });
                        var endTime = laydate.render({
                            elem: '#endTime'
                            ,type: 'datetime'
                            ,min: $("#startTime").val(),
                            done: function(value, date, endDate) {
                                endTime.config.min='1900-1-1';
                                    startTime.config.max=endTime.config.max;
                            }
                    });
                }else{
                    $('.layui-input-block').eq(4).html(planid.fil12)
                    $('.layui-input-block').eq(5).html(planid.fil13)
                }
                $('.layui-input-block').eq(6).html(planid.tcReqDuration)
                $('.xun div span').eq(0).html(planid.xjgen02)
                $('.xun div span').eq(1).html(planid.fil07)
                $('.xun div span').eq(2).html(planid.fil21)
                planDetails.forEach(item => {
                    let nextTable = ''
                    item.xjproject.forEach(elem => {
                        nextTable += `<table class="nextTable">
                                                <tbody>
                                                    <tr>
                                                        <td class="t">${elem.tcInspName}</td>
                                                        <td class="t" id="t">${elem.tcInspDsc}</td>
                                                        <td class="t">${elem.tcInspUnit}</td>
                                                        <td class="t">${elem.tcInspMax}</td>
                                                        <td class="t">${elem.tcInspMin}</td>
                                                        <td class="layui-form t">`
                        if (elem.tcProjStatus == 0) {
                            nextTable += `<select name="city" lay-verify="" style="display:block">
                                                                      <option value="0">否</option>
                                                                      <option value="1">是</option>
                                                                 </select>`
                        } else {
                            nextTable += `<select name="city" lay-verify="" style="display:block">
                                                                        <option value="1">是</option>
                                                                        <option value="0">否</option>
                                                                  </select>`
                        }
                        nextTable += `</td>
                        <td class="layui-form t">
                            <i style="color:red;position: absolute;left: 2px;top: 34px;font-size:15px;">*</i>
                            <input data-id='${elem.tcProjId}' data-tcProjDetailid=${item.tcDetailId} type="text" name="title"  placeholder="请填写" value= '${elem.tcProjDsc == null ? "" : elem.tcProjDsc}' class="layui-input">
                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>`
                    })
                    table += `<tr>
                                        <td>${item.fia01}</td>
                                        <td>${item.fia02}</td>
                                        <td>${item.fic02}</td>
                                        <td>${item.pmc03}</td>
                                        <td>${item.tcSeriesCode}</td>
                                        <td>${item.fii02}</td>
                                        <td>
                                            ${nextTable}
                                        </td>
                                    </tr>`
    
                })
                $('.layui-table tbody').html(table);
                }
            })
            $('.cancel').on('click', function () {
                setTimeout(function(){
                    location.hash = '/patrol/cash';
                },1000)
            })
            $('[data-jump="patrol/cash"]').addClass('layui-this');
        })
    
    </script>